package com.samphanie.auiu.biz_detail.biz.goods

import android.annotation.SuppressLint
import android.content.Context
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.samphanie.auiu.biz_detail.R
import com.samphanie.auiu.common.ui.view.loadUrl
import com.samphanie.auiu.pub_mod.model.GoodsDetailModel
import com.samphanie.auiu.pub_mod.model.GoodsModel
import com.samphanie.auiu.pub_mod.model.Shop
import com.samphanie.auiu.ui.item.AuiuAdapter
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder

/**
 * 店铺模块 = 基础信息 + 关联商品
 * @author ZSY
 */
class ShopItem(val model: GoodsDetailModel) : AuiuDataItem<GoodsDetailModel, AuiuViewHolder>() {

    private val SHOP_GOODS_ITEM_SOAN_COUNT = 3

    @SuppressLint("SetTextI18n")
    override fun onBindData(holder: AuiuViewHolder, position: Int) {
        val context = holder.itemView.context ?: return
        val shop: Shop? = model.shop
        shop?.let {
            holder.findViewById<ImageView>(R.id.shop_logo)?.loadUrl(it.logo)
            holder.findViewById<TextView>(R.id.shop_name)?.text = it.name
            val goodsNum = it.goodsNum
            val completedNum= it.completedNum
            holder.findViewById<TextView>(R.id.shop_desc)?.text =
                "商品数量: $goodsNum  已售: $completedNum"

            val evaluation: String? = shop.evaluation
            evaluation?.let {
                val tagContainer = holder.findViewById<LinearLayout>(R.id.tag_container)
                tagContainer!!.visibility = View.VISIBLE

                val serviceTags = evaluation.split(" ")
                var index = 0
                for (tagIndex in 0 until serviceTags.size / 2) {
                    val tagView: TextView = if (tagIndex < tagContainer.childCount) {
                        tagContainer.getChildAt(tagIndex) as TextView
                    } else {
                        val tagView = TextView(context)
                        val params =
                            LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT)
                        params.weight = 1f
                        tagView.layoutParams = params
                        tagView.gravity = Gravity.CENTER
                        tagView.textSize = 14f
                        tagView.setTextColor(ContextCompat.getColor(context, R.color.color_666))

                        tagContainer.addView(tagView)
                        tagView
                    }

                    val serviceName = serviceTags[index]
                    val serviceTag = serviceTags[index + 1]
                    index += 2

                    val spanTag = spanServiceTag(context, serviceName, serviceTag)
                    tagView.text = spanTag
                }
            }
        }

        val flowGoods: List<GoodsModel>? = model.flowGoods
        flowGoods.let {
            val flowRecyclerView: RecyclerView? = holder.findViewById(R.id.flow_recycle_view)
            flowRecyclerView!!.visibility = View.VISIBLE

            if (flowRecyclerView.layoutManager == null) {
                flowRecyclerView.layoutManager = GridLayoutManager(context, SHOP_GOODS_ITEM_SOAN_COUNT)
            }
            if (flowRecyclerView.adapter == null) {
                flowRecyclerView.adapter = AuiuAdapter(context)
            }

            val dataItems = mutableListOf<GoodsItem>()
            it?.forEach {
                dataItems.add(ShopGoodsItem(it))
            }
            val adapter = flowRecyclerView.adapter as AuiuAdapter
            adapter.clearItems()
            adapter.addItems(dataItems, true)
        }
    }

    private inner class ShopGoodsItem(goodsModel: GoodsModel): GoodsItem(goodsModel, false) {
        override fun getItemLayoutRes(): Int {
            return R.layout.layout_goods_detail_item_shop_goods_item
        }

        override fun onViewAttachedToWindow(holder: GoodsItemHolder) {
            super.onViewAttachedToWindow(holder)

            val viewParent: ViewGroup = holder.itemView.parent as ViewGroup
            val availableWidth = viewParent.measuredWidth - viewParent.paddingLeft - viewParent.paddingRight
            val itemWidth = availableWidth / SHOP_GOODS_ITEM_SOAN_COUNT

            val itemImage = holder.findViewById<ImageView>(R.id.item_image)
            val params = itemImage!!.layoutParams
            params.width = itemWidth
            params.height = itemWidth
            itemImage.layoutParams = params
        }
    }

    private fun spanServiceTag(
        context: Context,
        serviceName: String,
        serviceTag: String
    ): CharSequence {
        val ss = SpannableString(serviceTag)
        val ssb = SpannableStringBuilder()

        ss.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_c61)),
            0,
            ss.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        ss.setSpan(
            BackgroundColorSpan(ContextCompat.getColor(context, R.color.color_f8e)),
            0, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        ssb.append(serviceName)
        ssb.append(ss)
        return ssb
    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_item_shop
    }
}